const mongoose = require('mongoose');
const reg = require('../utils/reg');
const path = require('path');
// 密码加密
const bcrypt = require('../utils/bcrypt');

// 5.创建文档的结构对象(设置对象数据类型格式)
let Accountchema = new mongoose.Schema({
  creationTime: {//创建时间 毫秒
    type: Number,
    default: new Date(),
    required: true, //判断该字段必填
  },
  updatedAt: {//修改时间
    type: Number,
    default: new Date()
  },
  phone: {
    type: Number,
    required: true, //判断该字段必填
  },
  password: {
    type: String,
    set(val) {
      // 每次数据库进行修改或插入时都会执行set
      return bcrypt(val); // 对存入数据库的密码进行加密
    },
  },
  username: {
    type: String,
    default: '测试用户'
  },
  avatar: {
    type: String,
    default: '/image/logo.png',
  },
  userId: { type: mongoose.SchemaTypes.ObjectId, ref: 'friend' }
});
// 6.创建模型对象
let AccountModel = mongoose.model('user', Accountchema);

module.exports = AccountModel
